package Lee_code;

import java.util.Arrays;

/**
 * @author blh
 * @date 2024年08月02日 11:32
 */
public class Lee_416_82 {
    public boolean canPartition(int[] nums) {
        int len = nums.length;
        int sum = Arrays.stream(nums).sum();
        if (sum % 2 == 0) {
            sum = sum / 2;
        } else {
            return false;
        }

        int[] dp = new int[sum + 1];
        // 先物品后背包试一试
        for (int i = 0; i < nums.length; i++) {
            for (int j = 1; j <= sum; j++) {
                if (j >= nums[i]) {
                    dp[j] = Math.max(dp[j], dp[j - nums[i]] + nums[i]);
                }
            }
        }
        return dp[sum] == sum;
    }
}
